class Cartasur::Objective < Cartasur::CartasurModel
  has_many :protocols, :class_name => 'Cartasur::Protocol'
  has_many :users, :class_name => 'Cartasur::User', :through => :protocols
 #belongs_to :location, :class_name => 'Cartasur::Location'

  attr_accessible :time_range, :days, :months, :location_id, :location, :users, :protocols_attributes , :users_attributes

  accepts_nested_attributes_for :protocols, :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :users

  def location_name
    unless self.location.nil?
    Cartasur::Location.find(self.location).name
    else
      ""
    end
  end

  def location_coords
    unless self.location.nil?
      Cartasur::Location.find(self.location).aslatlng
    else
      ""
    end
  end

  def location_coordsarr
    ret = [0,0]
    unless self.location.nil?
      coords= Cartasur::Location.find(self.location).coordinates
      unless coords.nil?
       lat = coords.x
       lng = coords.y
       ret = [lat,lng]
      end
    end

    ret
  end


  def objective_details
    objective = Cartasur::Objective.find(self.objective_id)
    objective.time_range #TODO: return something readable
  end
end